package customerSelection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.TreeSet;

import entities.Customer;
import entities.Product;
import entities.ProductComparator;
import entities.Rule;

public class CustomerSelection 
{
	public static HashMap<Integer, Customer> customers;	
	public static ArrayList<Rule> rules;	
	public static HashMap<Integer, Product> products;
	
	//purchase history related information
	//the weight assigned based on the purchase history
	//newer purchase have higher weight
	//if the customer has a transaction in a given month then the weight is positive, else it is 0
	//the positive weight is based on how new the transaction is i.e. the newest month has weight 1, 
	//while the oldest has weight = 1/total nr. of months 
	//eg. - 5th month from the oldest would have a weight = 5/total nr. of months
	public static double purchaseHistoryWeight[];
	public static ArrayList<Integer> months;		//list of months - starting with oldest month at 0
	
	public static TreeMap<Product, ArrayList<Customer>> selectedCustomers;
	
	//number of top customers	
	public static int N;
	
	public static TreeMap<Product, ArrayList<Customer>> SelectCustomers(int divideByNLHS,int divideByNTotal, boolean usePaperMethod, int usePurchaseHistory)
	{	
		//calculate value1 for all products and customers
		//rank the products according to the aggregated value for top N customers
		TreeSet<Product> rankedProducts = new TreeSet<Product>(new ProductComparator());
		for(Customer customer : customers.values())
		{
			customer.calculateValue1(products.values(), divideByNLHS, divideByNTotal, usePurchaseHistory);
		}
		for(Product product : products.values()) 
		{
			if(product.getRulesWithProductAsAntecedent() != null)
			{
				product.getAggregatedValue1(N);
				rankedProducts.add(product);
			}
		}		
		
		//to store the top N customers for each product
		selectedCustomers = new TreeMap<Product, ArrayList<Customer>>(new ProductComparator());
		
		//top product
		Product topProduct = rankedProducts.pollLast();
		//top N customers for the top product
		ArrayList<Customer> topProductCustomers = new ArrayList<Customer>(topProduct.getTopNCustomers(N, true, usePaperMethod, divideByNLHS, divideByNTotal, usePurchaseHistory));
		selectedCustomers.put(topProduct, topProductCustomers);
		
		//printing
		System.out.print("Product: " + topProduct.getProductId());
		System.out.print(" Customers:");
		for (Customer customer : topProductCustomers) 
		{
			System.out.print(" " + customer.getCustomerId() + ",");					
		}
		System.out.println();
		
		while(rankedProducts.size() > 0)
		{
			Product product = rankedProducts.pollLast();
			Collection<Customer> productCustomers = product.getTopNCustomers(N, false, usePaperMethod, divideByNLHS, divideByNTotal, usePurchaseHistory);
			selectedCustomers.put(product, new ArrayList<Customer>(productCustomers));
			
			//printing
			System.out.print("Product: " + product.getProductId());
			System.out.print(" Customers:");
			for (Customer customer : productCustomers) 
			{
				System.out.print(" " + customer.getCustomerId() + ",");					
			}
			System.out.println();
		}
		
		return selectedCustomers;
	}
}
